<template>
  <div class="account-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>账号列表</span>
      </div>
      <el-table
        ref="acctable"
        :data="tableData"
        @selection-change="tableSeleted"
      >
        <el-table-column type="selection" width="75"></el-table-column>
        <el-table-column label="账号" width="150">
          <template slot-scope="scope">{{ scope.row.account }}</template>
        </el-table-column>
        <el-table-column prop="userGroup" label="用户组" width="150">
        </el-table-column>
        <el-table-column label="创建时间" width="150">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <span>{{ scope.row.ctime | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-row>
        <el-button type="danger" @click="delAll">批量删除</el-button>
        <el-button type="primary" @click="cancelSelect">取消选择</el-button>
      </el-row>
    </el-card>
    <!-- 模态框 -->
    <el-dialog title="修改账号" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="账号名" :label-width="formLabelWidth">
          <el-input
            v-model="editForm.account"
            autocomplete="off"
            placeholder="请输入账号名"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户组" :label-width="formLabelWidth">
          <el-select v-model="editForm.userGroup" placeholder="请选择用户组">
            <el-option label="普通管理员" value="普通管理员"></el-option>
            <el-option label="超级管理员" value="超级管理员"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="savaEditBtn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userList, userDel, userEdit, batchdel } from "../../api/user";
import dayjs from "dayjs";

export default {
  data() {
    return {
      ids: [], //批量删除的id号
      dialogFormVisible: false,
      editForm: {}, //修改的表单
      formLabelWidth: "120px",
      currentPage: 1, //当前页
      pageSize: 5, //每页显示的条数
      tableData: [], //表单数据
      total: 0, //总条数
    };
  },

  methods: {
    //多选框选中
    tableSeleted(selection) {
      this.ids = selection.map((item) => item.id);
    },
    //批量删除
    async delAll() {
      if (this.ids.length > 0) {
        let res = await batchdel({ ids: this.ids });
        let { code, msg } = res;
        if (code === 0) {
          this.$message({
            type: "success",
            message: msg,
          });
          this.loadData();
        } else {
          this.$message.error(msg);
        }
      }
    },
    //取消选择
    cancelSelect() {
      this.$refs.acctable.clearSelection();
    },
    //确定修改账号
    async savaEditBtn() {
      let res = await userEdit(this.editForm);
      let { code, msg } = res;
      if (code === 0) {
        this.$message({
          type: "success",
          message: msg,
        });
        this.loadData(); //重新加载数据
      } else {
        this.$message.error(msg);
      }
      this.dialogFormVisible = false;
    },
    //获取账号列表
    async loadData() {
      let res = await userList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
      let { data, total } = res;
      this.tableData = data;
      this.total = total;
      console.log(data);
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.editForm = { ...row };
      console.log(index, row);
    },

    //删除
    handleDelete(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该账号, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await userDel({ id }); //删除账号的接口
          console.log(res);
          this.loadData();
        })
        .catch(() => {
          console.log("取消");
        });
    },

    //更改每页条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.loadData();
    },

    //点其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.loadData();
    },
  },
  //调用函数,获取列表
  created() {
    this.loadData();
  },
  filters: {
    formatDate(val) {
      return dayjs(val).format("YYYY/MM/DD");
    },
  },
};
</script>

<style lang="less" scoped>
.account-list {
  background-color: #fff;
}
.el-pagination,
.el-row {
  margin-top: 20px;
}
</style>